package com.tensquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.tensquare.util.NotNullHelps;
import com.tensquare.web.util.WebConstant;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WebFilter extends ZuulFilter {

    private String type_before = "pre";

    @Override
    public String filterType() {
        return type_before;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    // 过滤器--头部信息转发
    @Override
    public Object run() throws ZuulException {
        System.out.println("@@ zuul过滤器--WebFilter... \n");
        RequestContext presentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = presentContext.getRequest();
        String header = request.getHeader(WebConstant.request_header);
        if (NotNullHelps.isNotNull(header) == true) {
            presentContext.addZuulRequestHeader(WebConstant.request_header, header);
        }
        return null;
    }
}
